import rcontrolutil
import rcontrol
import Tkinter
from pyrind.wrappers.scrolledlistbox import ScrolledListbox

class RListbox(rcontrol.RControl):
	"""
	Listbox widget. Pass in scrollbar=True to add a scrollbar.
	
	
	Properties:
	
	*scrollbar* - True or false. Display a vertical scrollbar. Can only be set when the control is first created, in the constructor.
	*items* - List of strings. Each string will be an item in the listbox.
	*multiple* - If True, users can use Shift or Control to select ranges of items.
	*enabled* - If False, the listbox is grayed out.
	
	*bgcolor* - Background color of the listbox. RGB triplet, such as (255,0,0) for pure red.
	*color* - Foreground color of the listbox. RGB triplet.
	*borderwidth* - An integer, width of border in pixels.
	*borderstyle* - Border style. One of 'sunken','raised','groove','ridge','flat'. Default is 'sunken'.
	*selectioncolor* - Color of text when an item is selected. RGB triplet.
	*selectionbgcolor* - Background color when an item is selected. RGB triplet.
	
	*width* - Set width of listbox, where one unit is the width of a char. Defaults to 20.
	*height* - Set height of listbox, how many items are displayed. Defaults to 10.
	*font* - Set font of items in the listbox. Standard tkinter font format, such as ('Times New Roman', 12, 'normal')
	
	"""
	#unfortunately, tkinter doesn't provide callback upon item changed; polling is needed
	
	
	def __init__(self, strText=None, parent=None, **options):
		self._newWidgetManual(parent, Tkinter.Listbox)
		
		if strText!=None: self.text = strText
		self.set_many(**options)
	
	def add_item(self, entry, bEnd=False):
		"""Append item to the current location, or to the end if bEnd is True."""
		if bEnd:
			self.basecontrol.insert(Tkinter.END, entry)
		else:
			self.basecontrol.insert(Tkinter.ACTIVE, entry)
		
	def insert_item(self, index, entry):
		"""Insert item at the given index."""
		self.basecontrol.insert(index, entry)
	
	def delete_item(self, index):
		"""Delete item at the given index."""
		self.basecontrol.delete(index)
	
	def style_item(self, index, **options):
		"""Style one item of the list, at the given index. background='blue', foreground='#0000ff', font, and so on."""
		self.basecontrol.itemconfig(index, **options)
		
	def style_item_get(self, index, option):
		"""Retrieve style information for one item of the list. background='blue', foreground='#0000ff', font, and so on."""
		return self.basecontrol.itemcget(index, option)	
	
	def clear(self):
		"""Clear all items."""
		self.basecontrol.delete(0,Tkinter.END)
	
	def get_selected_items(self):
		"""Returns list of strings, which represent the selected items. Returns empty list if there is no selection."""
		ret = []
		for index in self.get_selection():
			ret.append(self.basecontrol.get(index))
		return ret
	
	def get_selection(self):
		"""Returns list of integers, the indices of selected items. Returns empty list if there is no selection."""
		if len(self.basecontrol.curselection())==0:
			return []
		else:
			return map(int, self.basecontrol.curselection())
			
	def deselect(self):
		"""Deselect all items."""
		self.basecontrol.selection_clear(0,Tkinter.END)
		
	def set_selection(self,index):
		"""Select the item at the given index"""
		# If we aren't on select multiple, clear the selection first.
		if not self.multiple: self.deselect()
		self.basecontrol.selection_set(index)
	
	def scrollto_start(self):
		"""Scroll to start."""
		self.basecontrol.see(0)
		
	def scrollto_end(self):
		"""Scroll to end."""
		self.basecontrol.see(Tkinter.END)
	
	def property_enabled_get(self):
		if self._basecontrol['state']==Tkinter.DISABLED: return False
		else: return True
	def property_enabled_set(self, value):
		if value: self._basecontrol['state'] = Tkinter.NORMAL
		else: self._basecontrol['state'] = Tkinter.DISABLED


	def property_scrollbar_get(self):
		return self._scrollbar
	def property_scrollbar_set(self, value):
		raise rcontrolutil.PyRindPropertyException( 'Property can only be set when first created.')
	

	
	def property_multiple_get(self):
		mode=self._basecontrol['selectmode']
		if mode==Tkinter.EXTENDED or mode==Tkinter.MULTIPLE: return True
		else: return False
	def property_multiple_set(self,value):
		if value: self._basecontrol['selectmode'] = Tkinter.EXTENDED
		else: self._basecontrol['selectmode'] = Tkinter.BROWSE
			
	def property_items_get(self):
		return self._basecontrol.get(0,Tkinter.END)
	def property_items_set(self,value):
		self._basecontrol.delete(0,Tkinter.END)
		for item in value:
			self._basecontrol.insert(Tkinter.END, item)
	
	
	_properties = {
		'items': (property_items_get, property_items_set),
		'scrollbar': (property_scrollbar_get, property_scrollbar_set),
	
		'multiple':(property_multiple_get, property_multiple_set),
		'enabled':(property_enabled_get, property_enabled_set),
		
		'bgcolor': ('background',rcontrolutil.mapColor),
		'color': ('foreground',rcontrolutil.mapColor),
		'selectioncolor': ('selectforeground',rcontrolutil.mapColor),
		'selectionbgcolor': ('selectbackground',rcontrolutil.mapColor),
		'borderwidth': 'borderwidth',
		
		'borderstyle': ('relief',rcontrolutil.mapBorderstyle),
		'width': 'width',
		'height': 'height',
		'font': 'font',
		
		
		}



